package com.akk.platform.manage;

import com.akk.module.avs.entity.AvsPayAccess;
import com.akk.module.avs.entity.AvsPayVipConfig;
import com.akk.module.avs.model.response.AvsPayAccessRes;
import com.akk.module.avs.service.AvsPayAccessService;
import com.akk.module.avs.service.AvsPayVipConfigService;
import com.akk.utils.RestResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/platform/manage/payConfig")
public class AvsPayConfigAction {

    @Autowired
    private AvsPayVipConfigService avsPayVipConfigService;

    @Autowired
    private AvsPayAccessService avsPayAccessService;

    @RequestMapping(value = "/get")
    @RequiresPermissions("platform:manage:pay:config")
    public RestResponse getInfo(){
        List<AvsPayVipConfig> configs = avsPayVipConfigService.selectAll();
        if (configs.size() == 0){
            init();
            return getInfo();
        }
        return RestResponse.build(configs.get(0));
    }

    @RequestMapping(value = "/getAccess")
    @RequiresPermissions("platform:manage:pay:config")
    public RestResponse getAccess(){
        List<AvsPayAccessRes> avsPayAccesses = avsPayAccessService.accessList();
        return RestResponse.build(avsPayAccesses);
    }

    @RequestMapping(value = "/update")
    @RequiresPermissions("platform:manage:pay:update")
    public RestResponse update(@RequestBody AvsPayVipConfig config){
        avsPayVipConfigService.update(config);
        return RestResponse.build("success");
    }

    private void init(){
        AvsPayVipConfig config = new AvsPayVipConfig();
        config.setBackEndDate(new Date());
        config.setBackMoney(0.0);
        config.setCountBack(0.0);
        config.setDiscountEndDate(new Date());
        config.setSettleAccess("0");
        config.setVipDiscount(0.0);
        config.setVipMoney(0.0);
        config.setIsOpen(1);
        avsPayVipConfigService.save(config);
    }

}
